import { contextBridge, ipcRenderer } from 'electron'

// 暴露API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  process: {
    versions: process.versions
  },
  ffmpeg: {
    convertToM3U8: (inputFile: string, outputDir: string) => 
      ipcRenderer.invoke('ffmpeg:convert-to-m3u8', inputFile, outputDir),
    
    checkAvailability: () => 
      ipcRenderer.invoke('ffmpeg:check-availability'),
    
    onLog: (callback: (log: string) => void) => 
      ipcRenderer.on('ffmpeg:log', (_, log) => callback(log)),
    
    extractPanoramaFrames: (inputFile: string, outputDir: string, interval?: number, resolution?: string) => 
      ipcRenderer.invoke('ffmpeg:extract-panorama-frames', inputFile, outputDir, interval, resolution),
    
    extractPanoramaProjections: (inputFile: string, outputDir: string, interval?: number, projectionType?: '5views' | '8views', resolution?: string) => 
      ipcRenderer.invoke('ffmpeg:extract-panorama-projections', inputFile, outputDir, interval, projectionType, resolution),
    
    extractPanoramaPhotoProjections: (inputDir: string, outputDir: string, projectionType: '5views' | '8views', resolution?: string) => 
      ipcRenderer.invoke('ffmpeg:extract-panorama-photo-projections', inputDir, outputDir, projectionType, resolution)
  },
  // 添加文件系统API
  fs: {
    existsSync: (path: string) => ipcRenderer.invoke('fs:exists-sync', path),
    readFile: (path: string) => ipcRenderer.invoke('fs:read-file', path)
  },
  // 添加对话框API
  dialog: {
    showOpenDialog: (options: any) => ipcRenderer.invoke('dialog:show-open-dialog', options),
    openFile: (options: any) => ipcRenderer.invoke('dialog:open-file', options),
    openDirectory: () => ipcRenderer.invoke('dialog:open-directory')
  },
  // 窗口相关API
  window: {
    openAbout: () => ipcRenderer.invoke('window:open-about')
  }
})

// 类型定义
export interface ElectronAPI {
  process: {
    versions: NodeJS.ProcessVersions
  },
  ffmpeg: {
    convertToM3U8: (inputFile: string, outputDir: string) => Promise<{success: boolean, error?: string}>,
    checkAvailability: () => Promise<boolean>,
    onLog: (callback: (log: string) => void) => void,
    extractPanoramaFrames: (inputFile: string, outputDir: string, interval?: number, resolution?: string) => Promise<{success: boolean, error?: string}>,
    extractPanoramaProjections: (inputFile: string, outputDir: string, interval?: number, projectionType?: '5views' | '8views', resolution?: string) => Promise<{success: boolean, error?: string}>,
    extractPanoramaPhotoProjections: (inputDir: string, outputDir: string, projectionType: '5views' | '8views', resolution?: string) => Promise<{success: boolean, error?: string}>
  },
  fs: {
    existsSync: (path: string) => Promise<boolean>
    readFile: (path: string) => Promise<Buffer>
  },
  dialog: {
    showOpenDialog: (options: any) => Promise<any>,
    openFile: (options: any) => Promise<string | null>,
    openDirectory: () => Promise<string | null>
  },
  window: {
    openAbout: () => Promise<{success: boolean}>
  }
}

declare global {
  interface Window {
    electronAPI: ElectronAPI
  }
}